home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / stack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-16  |  32.9 KB  |  1,268 lines

  1. /* Print and select stack frames for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "value.h"
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "expression.h"
  25. #include "language.h"
  26. #include "frame.h"
  27. #include "gdbcmd.h"
  28. #include "gdbcore.h"
  29. #include "target.h"
  30. #include "breakpoint.h"
  31.  
  32. static void
  33. return_command PARAMS ((char *, int));
  34.  
  35. static void
  36. down_command PARAMS ((char *, int));
  37.  
  38. static void
  39. down_silently_command PARAMS ((char *, int));
  40.  
  41. static void
  42. up_command PARAMS ((char *, int));
  43.  
  44. static void
  45. up_silently_command PARAMS ((char *, int));
  46.  
  47. static void
  48. frame_command PARAMS ((char *, int));
  49.  
  50. static void
  51. select_frame_command PARAMS ((char *, int));
  52.  
  53. static void
  54. args_info PARAMS ((char *, int));
  55.  
  56. static void
  57. print_frame_arg_vars PARAMS ((FRAME, FILE *));
  58.  
  59. static void
  60. catch_info PARAMS ((char *, int));
  61.  
  62. static void
  63. locals_info PARAMS ((char *, int));
  64.  
  65. static void
  66. print_frame_label_vars PARAMS ((FRAME, int, FILE *));
  67.  
  68. static void
  69. print_frame_local_vars PARAMS ((FRAME, FILE *));
  70.  
  71. static int
  72. print_block_frame_labels PARAMS ((struct block *, int *, FILE *));
  73.  
  74. static int
  75. print_block_frame_locals PARAMS ((struct block *, FRAME, FILE *));
  76.  
  77. static void
  78. backtrace_command PARAMS ((char *, int));
  79.  
  80. static FRAME
  81. parse_frame_specification PARAMS ((char *));
  82.  
  83. static void
  84. frame_info PARAMS ((char *, int));
  85.  
  86.  
  87. extern int addressprint;    /* Print addresses, or stay symbolic only? */
  88. extern int info_verbose;    /* Verbosity of symbol reading msgs */
  89. extern int lines_to_list;    /* # of lines "list" command shows by default */
  90. extern char *reg_names[];    /* Names of registers */
  91.  
  92. /* Thie "selected" stack frame is used by default for local and arg access.
  93.    May be zero, for no selected frame.  */
  94.  
  95. FRAME selected_frame;
  96.  
  97. /* Level of the selected frame:
  98.    0 for innermost, 1 for its caller, ...
  99.    or -1 for frame specified by address with no defined level.  */
  100.  
  101. int selected_frame_level;
  102.  
  103. /* Nonzero means print the full filename and linenumber
  104.    when a frame is printed, and do so in a format programs can parse.  */
  105.  
  106. int frame_file_full_name = 0;
  107.  
  108.  
  109. /* Print a stack frame briefly.  FRAME should be the frame id
  110.    and LEVEL should be its level in the stack (or -1 for level not defined).
  111.    This prints the level, the function executing, the arguments,
  112.    and the file name and line number.
  113.    If the pc is not at the beginning of the source line,
  114.    the actual pc is printed at the beginning.
  115.  
  116.    If SOURCE is 1, print the source line as well.
  117.    If SOURCE is -1, print ONLY the source line.  */
  118.  
  119. void
  120. print_stack_frame (frame, level, source)
  121.      FRAME frame;
  122.      int level;
  123.      int source;
  124. {
  125.   struct frame_info *fi;
  126.  
  127.   fi = get_frame_info (frame);
  128.  
  129.   print_frame_info (fi, level, source, 1);
  130. }
  131.  
  132. void
  133. print_frame_info (fi, level, source, args)
  134.      struct frame_info *fi;
  135.      register int level;
  136.      int source;
  137.      int args;
  138. {
  139.   struct symtab_and_line sal;
  140.   struct symbol *func;
  141.   register char *funname = 0;
  142.   int numargs;
  143.  
  144. #if 0    /* Symbol reading is fast enough now */
  145.   struct partial_symtab *pst;
  146.  
  147.   /* Don't give very much information if we haven't readin the
  148.      symbol table yet.  */
  149.   pst = find_pc_psymtab (fi->pc);
  150.   if (pst && !pst->readin)
  151.     {
  152.       /* Abbreviated information.  */
  153.       char *fname;
  154.  
  155.       if (!find_pc_partial_function (fi->pc, &fname, 0))
  156.     fname = "??";
  157.     
  158.       printf_filtered ("#%-2d ", level);
  159.       if (addressprint)
  160.         printf_filtered ("%s in ", local_hex_string(fi->pc));
  161.  
  162.       fputs_demangled (fname, stdout, -1);
  163.       fputs_filtered (" (...)\n", stdout);
  164.       
  165.       return;
  166.     }
  167. #endif
  168.  
  169. #ifdef CORE_NEEDS_RELOCATION
  170.   CORE_NEEDS_RELOCATION(fi->pc);
  171. #endif
  172.  
  173.   sal = find_pc_line (fi->pc, fi->next_frame);
  174.   func = find_pc_function (fi->pc);
  175.   if (func)
  176.     {
  177.       /* In certain pathological cases, the symtabs give the wrong
  178.      function (when we are in the first function in a file which
  179.      is compiled without debugging symbols, the previous function
  180.      is compiled with debugging symbols, and the "foo.o" symbol
  181.      that is supposed to tell us where the file with debugging symbols
  182.      ends has been truncated by ar because it is longer than 15
  183.      characters).
  184.  
  185.      So look in the minimal symbol tables as well, and if it comes
  186.      up with a larger address for the function use that instead.
  187.      I don't think this can ever cause any problems; there shouldn't
  188.      be any minimal symbols in the middle of a function.
  189.      FIXME:  (Not necessarily true.  What about text labels) */
  190.  
  191.       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
  192.       if (msymbol != NULL
  193.       && (msymbol -> address
  194.           > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
  195.     {
  196.       /* In this case we have no way of knowing the source file
  197.          and line number, so don't print them.  */
  198.       sal.symtab = 0;
  199.       /* We also don't know anything about the function besides
  200.          its address and name.  */
  201.       func = 0;
  202.       funname = msymbol -> name;
  203.     }
  204.       else
  205.     funname = SYMBOL_NAME (func);
  206.     }
  207.   else
  208.     {
  209.       register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
  210.       if (msymbol != NULL)
  211.     funname = msymbol -> name;
  212.     }
  213.  
  214.   if (source >= 0 || !sal.symtab)
  215.     {
  216.       if (level >= 0)
  217.     printf_filtered ("#%-2d ", level);
  218.       if (addressprint)
  219.     if (fi->pc != sal.pc || !sal.symtab)
  220.       printf_filtered ("%s in ", local_hex_string(fi->pc));
  221.       fputs_demangled (funname ? funname : "??", stdout, -1);
  222.       wrap_here ("   ");
  223.       fputs_filtered (" (", stdout);
  224.       if (args)
  225.     {
  226.       FRAME_NUM_ARGS (numargs, fi);
  227.       print_frame_args (func, fi, numargs, stdout);
  228.     }
  229.       printf_filtered (")");
  230.       if (sal.symtab && sal.symtab->filename)
  231.     {
  232.           wrap_here ("   ");
  233.       printf_filtered (" at %s:%d", sal.symtab->filename, sal.line);
  234.     }
  235.  
  236. #ifdef PC_LOAD_SEGMENT
  237.      /* If we couldn't print out function name but if can figure out what
  238.         load segment this pc value is from, at least print out some info
  239.     about its load segment. */
  240.       if (!funname) {
  241.     wrap_here ("  ");
  242.     printf_filtered (" from %s", PC_LOAD_SEGMENT (fi->pc));
  243.       }
  244. #endif
  245.       printf_filtered ("\n");
  246.     }
  247.  
  248.   if ((source != 0) && sal.symtab)
  249.     {
  250.       int done = 0;
  251.       int mid_statement = source < 0 && fi->pc != sal.pc;
  252.       if (frame_file_full_name)
  253.     done = identify_source_line (sal.symtab, sal.line, mid_statement);
  254.       if (!done)
  255.     {
  256.       if (addressprint && mid_statement)
  257.         printf_filtered ("%s\t", local_hex_string(fi->pc));
  258.       print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
  259.     }
  260.       current_source_line = max (sal.line - lines_to_list/2, 1);
  261.     }
  262.   if (source != 0)
  263.     set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
  264.  
  265.   fflush (stdout);
  266. }
  267.  
  268. #ifdef FRAME_SPECIFICATION_DYADIC
  269. extern FRAME setup_arbitrary_frame ();
  270. #endif
  271.  
  272. /*
  273.  * Read a frame specification in whatever the appropriate format is.
  274.  * Call error() if the specification is in any way invalid (i.e.
  275.  * this function never returns NULL).
  276.  */
  277. static FRAME
  278. parse_frame_specification (frame_exp)
  279.      char *frame_exp;
  280. {
  281.   int numargs = 0;
  282.   int arg1, arg2;
  283.   
  284.   if (frame_exp)
  285.     {
  286.       char *addr_string, *p;
  287.       struct cleanup *tmp_cleanup;
  288.  
  289.       while (*frame_exp == ' ') frame_exp++;
  290.       for (p = frame_exp; *p && *p != ' '; p++)
  291.     ;
  292.  
  293.       if (*frame_exp)
  294.     {
  295.       numargs = 1;
  296.       addr_string = savestring(frame_exp, p - frame_exp);
  297.  
  298.       {
  299.         tmp_cleanup = make_cleanup (free, addr_string);
  300.         arg1 = parse_and_eval_address (addr_string);
  301.         do_cleanups (tmp_cleanup);
  302.       }
  303.  
  304.       while (*p == ' ') p++;
  305.       
  306.       if (*p)
  307.         {
  308.           numargs = 2;
  309.           arg2 = parse_and_eval_address (p);
  310.         }
  311.     }
  312.     }
  313.  
  314.   switch (numargs)
  315.     {
  316.     case 0:
  317.       if (selected_frame == NULL)
  318.     error ("No selected frame.");
  319.       return selected_frame;
  320.       /* NOTREACHED */
  321.     case 1:
  322.       {
  323.     int level = arg1;
  324.     FRAME fid = find_relative_frame (get_current_frame (), &level);
  325.     FRAME tfid;
  326.  
  327.     if (level == 0)
  328.       /* find_relative_frame was successful */
  329.       return fid;
  330.  
  331.     /* If (s)he specifies the frame with an address, he deserves what
  332.        (s)he gets.  Still, give the highest one that matches.  */
  333.  
  334.     for (fid = get_current_frame ();
  335.          fid && FRAME_FP (fid) != arg1;
  336.          fid = get_prev_frame (fid))
  337.       ;
  338.  
  339.     if (fid)
  340.       while ((tfid = get_prev_frame (fid)) &&
  341.          (FRAME_FP (tfid) == arg1))
  342.         fid = tfid;
  343.       
  344. #ifdef FRAME_SPECIFICATION_DYADIC
  345.     if (!fid)
  346.       error ("Incorrect number of args in frame specification");
  347.  
  348.     return fid;
  349. #else
  350.     return create_new_frame (arg1, 0);
  351. #endif
  352.       }
  353.       /* NOTREACHED */
  354.     case 2:
  355.       /* Must be addresses */
  356. #ifndef FRAME_SPECIFICATION_DYADIC
  357.       error ("Incorrect number of args in frame specification");
  358. #else
  359.       return setup_arbitrary_frame (arg1, arg2);
  360. #endif
  361.       /* NOTREACHED */
  362.     }
  363.   fatal ("Internal: Error in parsing in parse_frame_specification");
  364.   /* NOTREACHED */
  365. }
  366.  
  367. /* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
  368.    that if it is unsure about the answer, it returns 0
  369.    instead of guessing (this happens on the VAX and i960, for example).
  370.  
  371.    On most machines, we never have to guess about the args address,
  372.    so FRAME_ARGS_ADDRESS{,_CORRECT} are the same.  */
  373. #if !defined (FRAME_ARGS_ADDRESS_CORRECT)
  374. #define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
  375. #endif
  376.  
  377. /* Print verbosely the selected frame or the frame at address ADDR.
  378.    This means absolutely all information in the frame is printed.  */
  379.  
  380. static void
  381. frame_info (addr_exp, from_tty)
  382.      char *addr_exp;
  383.      int from_tty;
  384. {
  385.   FRAME frame;
  386.   struct frame_info *fi;
  387.   struct frame_saved_regs fsr;
  388.   struct symtab_and_line sal;
  389.   struct symbol *func;
  390.   struct symtab *s;
  391.   FRAME calling_frame;
  392.   int i, count;
  393.   char *funname = 0;
  394.  
  395.   if (!target_has_stack)
  396.     error ("No inferior or core file.");
  397.  
  398.   frame = parse_frame_specification (addr_exp);
  399.   if (!frame)
  400.     error ("Invalid frame specified.");
  401.  
  402.   fi = get_frame_info (frame);
  403.   sal = find_pc_line (fi->pc, fi->next_frame);
  404.   func = get_frame_function (frame);
  405.   s = find_pc_symtab(fi->pc);
  406.   if (func)
  407.     funname = SYMBOL_NAME (func);
  408.   else
  409.     {
  410.       register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
  411.       if (msymbol != NULL)
  412.     funname = msymbol -> name;
  413.     }
  414.   calling_frame = get_prev_frame (frame);
  415.  
  416.   if (!addr_exp && selected_frame_level >= 0) {
  417.     printf_filtered ("Stack level %d, frame at %s:\n",
  418.              selected_frame_level, 
  419.              local_hex_string(FRAME_FP(frame)));
  420.   } else {
  421.     printf_filtered ("Stack frame at %s:\n",
  422.              local_hex_string(FRAME_FP(frame)));
  423.   }
  424.   printf_filtered (" %s = %s",
  425.            reg_names[PC_REGNUM], 
  426.            local_hex_string(fi->pc));
  427.  
  428.   wrap_here ("   ");
  429.   if (funname)
  430.     {
  431.       printf_filtered (" in ");
  432.       fputs_demangled (funname, stdout, 1);
  433.     }
  434.   wrap_here ("   ");
  435.   if (sal.symtab)
  436.     printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
  437.   puts_filtered ("; ");
  438.   wrap_here ("    ");
  439.   printf_filtered ("saved %s %s\n", reg_names[PC_REGNUM],
  440.            local_hex_string(FRAME_SAVED_PC (frame)));
  441.  
  442.   {
  443.     int frameless = 0;
  444. #ifdef FRAMELESS_FUNCTION_INVOCATION
  445.     FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
  446. #endif
  447.     if (frameless)
  448.       printf_filtered (" (FRAMELESS),");
  449.   }
  450.  
  451.   if (calling_frame)
  452.     printf_filtered (" called by frame at %s", 
  453.              local_hex_string(FRAME_FP (calling_frame)));
  454.   if (fi->next_frame && calling_frame)
  455.     puts_filtered (",");
  456.   wrap_here ("   ");
  457.   if (fi->next_frame)
  458.     printf_filtered (" caller of frame at %s", local_hex_string(fi->next_frame));
  459.   if (fi->next_frame || calling_frame)
  460.     puts_filtered ("\n");
  461.   if (s)
  462.      printf_filtered(" source language %s.\n", language_str(s->language));
  463.  
  464.   {
  465.     /* Address of the argument list for this frame, or 0.  */
  466.     CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
  467.     /* Number of args for this frame, or -1 if unknown.  */
  468.     int numargs;
  469.  
  470.     if (arg_list == 0)
  471.     printf_filtered (" Arglist at unknown address.\n");
  472.     else
  473.       {
  474.     printf_filtered (" Arglist at %s,", local_hex_string(arg_list));
  475.  
  476.     FRAME_NUM_ARGS (numargs, fi);
  477.     if (numargs < 0)
  478.       puts_filtered (" args: ");
  479.     else if (numargs == 0)
  480.       puts_filtered (" no args.");
  481.     else if (numargs == 1)
  482.       puts_filtered (" 1 arg: ");
  483.     else
  484.       printf_filtered (" %d args: ", numargs);
  485.     print_frame_args (func, fi, numargs, stdout);
  486.     puts_filtered ("\n");
  487.       }
  488.   }
  489.   {
  490.     /* Address of the local variables for this frame, or 0.  */
  491.     CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
  492.  
  493.     if (arg_list == 0)
  494.     printf_filtered (" Locals at unknown address,");
  495.     else
  496.     printf_filtered (" Locals at %s,", local_hex_string(arg_list));
  497.   }
  498.  
  499. #if defined (FRAME_FIND_SAVED_REGS)  
  500.   get_frame_saved_regs (fi, &fsr);
  501.   /* The sp is special; what's returned isn't the save address, but
  502.      actually the value of the previous frame's sp.  */
  503.   printf_filtered (" Previous frame's sp is %s\n", 
  504.            local_hex_string(fsr.regs[SP_REGNUM]));
  505.   count = 0;
  506.   for (i = 0; i < NUM_REGS; i++)
  507.     if (fsr.regs[i] && i != SP_REGNUM)
  508.       {
  509.     if (count == 0)
  510.       puts_filtered (" Saved registers:\n ");
  511.     else
  512.       puts_filtered (",");
  513.     wrap_here (" ");
  514.     printf_filtered (" %s at %s", reg_names[i], 
  515.              local_hex_string(fsr.regs[i]));
  516.     count++;
  517.       }
  518.   if (count)
  519.     puts_filtered ("\n");
  520. #endif /* Have FRAME_FIND_SAVED_REGS.  */
  521. }
  522.  
  523. #if 0
  524. /* Set a limit on the number of frames printed by default in a
  525.    backtrace.  */
  526.  
  527. static int backtrace_limit;
  528.  
  529. static void
  530. set_backtrace_limit_command (count_exp, from_tty)
  531.      char *count_exp;
  532.      int from_tty;
  533. {
  534.   int count = parse_and_eval_address (count_exp);
  535.  
  536.   if (count < 0)
  537.     error ("Negative argument not meaningful as backtrace limit.");
  538.  
  539.   backtrace_limit = count;
  540. }
  541.  
  542. static void
  543. backtrace_limit_info (arg, from_tty)
  544.      char *arg;
  545.      int from_tty;
  546. {
  547.   if (arg)
  548.     error ("\"Info backtrace-limit\" takes no arguments.");
  549.  
  550.   printf ("Backtrace limit: %d.\n", backtrace_limit);
  551. }
  552. #endif
  553.  
  554. /* Print briefly all stack frames or just the innermost COUNT frames.  */
  555.  
  556. static void
  557. backtrace_command (count_exp, from_tty)
  558.      char *count_exp;
  559.      int from_tty;
  560. {
  561.   struct frame_info *fi;
  562.   register int count;
  563.   register FRAME frame;
  564.   register int i;
  565.   register FRAME trailing;
  566.   register int trailing_level;
  567.  
  568.   if (!target_has_stack)
  569.     error ("No stack.");
  570.  
  571.   /* The following code must do two things.  First, it must
  572.      set the variable TRAILING to the frame from which we should start
  573.      printing.  Second, it must set the variable count to the number
  574.      of frames which we should print, or -1 if all of them.  */
  575.   trailing = get_current_frame ();
  576.   trailing_level = 0;
  577.   if (count_exp)
  578.     {
  579.       count = parse_and_eval_address (count_exp);
  580.       if (count < 0)
  581.     {
  582.       FRAME current;
  583.  
  584.       count = -count;
  585.  
  586.       current = trailing;
  587.       while (current && count--)
  588.         {
  589.           QUIT;
  590.           current = get_prev_frame (current);
  591.         }
  592.       
  593.       /* Will stop when CURRENT reaches the top of the stack.  TRAILING
  594.          will be COUNT below it.  */
  595.       while (current)
  596.         {
  597.           QUIT;
  598.           trailing = get_prev_frame (trailing);
  599.           current = get_prev_frame (current);
  600.           trailing_level++;
  601.         }
  602.       
  603.       count = -1;
  604.     }
  605.     }
  606.   else
  607.     count = -1;
  608.  
  609.   if (info_verbose)
  610.     {
  611.       struct partial_symtab *ps;
  612.       
  613.       /* Read in symbols for all of the frames.  Need to do this in
  614.      a separate pass so that "Reading in symbols for xxx" messages
  615.      don't screw up the appearance of the backtrace.  Also
  616.      if people have strong opinions against reading symbols for
  617.      backtrace this may have to be an option.  */
  618.       i = count;
  619.       for (frame = trailing;
  620.        frame != NULL && i--;
  621.        frame = get_prev_frame (frame))
  622.     {
  623.       QUIT;
  624.       fi = get_frame_info (frame);
  625.       ps = find_pc_psymtab (fi->pc);
  626.       if (ps)
  627.         (void) PSYMTAB_TO_SYMTAB (ps);    /* Force syms to come in */
  628.     }
  629.     }
  630.  
  631.   for (i = 0, frame = trailing;
  632.        frame && count--;
  633.        i++, frame = get_prev_frame (frame))
  634.     {
  635.       QUIT;
  636.       fi = get_frame_info (frame);
  637.       print_frame_info (fi, trailing_level + i, 0, 1);
  638.     }
  639.  
  640.   /* If we've stopped before the end, mention that.  */
  641.   if (frame && from_tty)
  642.     printf_filtered ("(More stack frames follow...)\n");
  643. }
  644.  
  645. /* Print the local variables of a block B active in FRAME.
  646.    Return 1 if any variables were printed; 0 otherwise.  */
  647.  
  648. static int
  649. print_block_frame_locals (b, frame, stream)
  650.      struct block *b;
  651.      register FRAME frame;
  652.      register FILE *stream;
  653. {
  654.   int nsyms;
  655.   register int i;
  656.   register struct symbol *sym;
  657.   register int values_printed = 0;
  658.  
  659.   nsyms = BLOCK_NSYMS (b);
  660.  
  661.   for (i = 0; i < nsyms; i++)
  662.     {
  663.       sym = BLOCK_SYM (b, i);
  664.       if (SYMBOL_CLASS (sym) == LOC_LOCAL
  665.       || SYMBOL_CLASS (sym) == LOC_REGISTER
  666.       || SYMBOL_CLASS (sym) == LOC_STATIC)
  667.     {
  668.       values_printed = 1;
  669.       fprint_symbol (stream, SYMBOL_NAME (sym));
  670.       fputs_filtered (" = ", stream);
  671.       print_variable_value (sym, frame, stream);
  672.       fprintf_filtered (stream, "\n");
  673.     }
  674.     }
  675.   return values_printed;
  676. }
  677.  
  678. /* Same, but print labels.  */
  679.  
  680. static int
  681. print_block_frame_labels (b, have_default, stream)
  682.      struct block *b;
  683.      int *have_default;
  684.      register FILE *stream;
  685. {
  686.   int nsyms;
  687.   register int i;
  688.   register struct symbol *sym;
  689.   register int values_printed = 0;
  690.  
  691.   nsyms = BLOCK_NSYMS (b);
  692.  
  693.   for (i = 0; i < nsyms; i++)
  694.     {
  695.       sym = BLOCK_SYM (b, i);
  696.       if (! strcmp (SYMBOL_NAME (sym), "default"))
  697.     {
  698.       if (*have_default)
  699.         continue;
  700.       *have_default = 1;
  701.     }
  702.       if (SYMBOL_CLASS (sym) == LOC_LABEL)
  703.     {
  704.       struct symtab_and_line sal;
  705.       sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
  706.       values_printed = 1;
  707.       fputs_demangled (SYMBOL_NAME (sym), stream, 1);
  708.       if (addressprint)
  709.         fprintf_filtered (stream, " %s", 
  710.                   local_hex_string(SYMBOL_VALUE_ADDRESS (sym)));
  711.       fprintf_filtered (stream, " in file %s, line %d\n",
  712.                 sal.symtab->filename, sal.line);
  713.     }
  714.     }
  715.   return values_printed;
  716. }
  717.  
  718. /* Print on STREAM all the local variables in frame FRAME,
  719.    including all the blocks active in that frame
  720.    at its current pc.
  721.  
  722.    Returns 1 if the job was done,
  723.    or 0 if nothing was printed because we have no info
  724.    on the function running in FRAME.  */
  725.  
  726. static void
  727. print_frame_local_vars (frame, stream)
  728.      register FRAME frame;
  729.      register FILE *stream;
  730. {
  731.   register struct block *block = get_frame_block (frame);
  732.   register int values_printed = 0;
  733.  
  734.   if (block == 0)
  735.     {
  736.       fprintf_filtered (stream, "No symbol table info available.\n");
  737.       return;
  738.     }
  739.   
  740.   while (block != 0)
  741.     {
  742.       if (print_block_frame_locals (block, frame, stream))
  743.     values_printed = 1;
  744.       /* After handling the function's top-level block, stop.
  745.      Don't continue to its superblock, the block of
  746.      per-file symbols.  */
  747.       if (BLOCK_FUNCTION (block))
  748.     break;
  749.       block = BLOCK_SUPERBLOCK (block);
  750.     }
  751.  
  752.   if (!values_printed)
  753.     {
  754.       fprintf_filtered (stream, "No locals.\n");
  755.     }
  756. }
  757.  
  758. /* Same, but print labels.  */
  759.  
  760. static void
  761. print_frame_label_vars (frame, this_level_only, stream)
  762.      register FRAME frame;
  763.      int this_level_only;
  764.      register FILE *stream;
  765. {
  766.   register struct blockvector *bl;
  767.   register struct block *block = get_frame_block (frame);
  768.   register int values_printed = 0;
  769.   int index, have_default = 0;
  770.   char *blocks_printed;
  771.   struct frame_info *fi = get_frame_info (frame);
  772.   CORE_ADDR pc = fi->pc;
  773.  
  774.   if (block == 0)
  775.     {
  776.       fprintf_filtered (stream, "No symbol table info available.\n");
  777.       return;
  778.     }
  779.  
  780.   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
  781.   blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
  782.   bzero (blocks_printed, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
  783.  
  784.   while (block != 0)
  785.     {
  786.       CORE_ADDR end = BLOCK_END (block) - 4;
  787.       int last_index;
  788.  
  789.       if (bl != blockvector_for_pc (end, &index))
  790.     error ("blockvector blotch");
  791.       if (BLOCKVECTOR_BLOCK (bl, index) != block)
  792.     error ("blockvector botch");
  793.       last_index = BLOCKVECTOR_NBLOCKS (bl);
  794.       index += 1;
  795.  
  796.       /* Don't print out blocks that have gone by.  */
  797.       while (index < last_index
  798.          && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
  799.     index++;
  800.  
  801.       while (index < last_index
  802.          && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
  803.     {
  804.       if (blocks_printed[index] == 0)
  805.         {
  806.           if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
  807.         values_printed = 1;
  808.           blocks_printed[index] = 1;
  809.         }
  810.       index++;
  811.     }
  812.       if (have_default)
  813.     return;
  814.       if (values_printed && this_level_only)
  815.     return;
  816.  
  817.       /* After handling the function's top-level block, stop.
  818.      Don't continue to its superblock, the block of
  819.      per-file symbols.  */
  820.       if (BLOCK_FUNCTION (block))
  821.     break;
  822.       block = BLOCK_SUPERBLOCK (block);
  823.     }
  824.  
  825.   if (!values_printed && !this_level_only)
  826.     {
  827.       fprintf_filtered (stream, "No catches.\n");
  828.     }
  829. }
  830.  
  831. /* ARGSUSED */
  832. static void
  833. locals_info (args, from_tty)
  834.      char *args;
  835.      int from_tty;
  836. {
  837.   if (!selected_frame)
  838.     error ("No frame selected.");
  839.   print_frame_local_vars (selected_frame, stdout);
  840. }
  841.  
  842. static void
  843. catch_info (ignore, from_tty)
  844.      char *ignore;
  845.      int from_tty;
  846. {
  847.   if (!selected_frame)
  848.     error ("No frame selected.");
  849.   print_frame_label_vars (selected_frame, 0, stdout);
  850. }
  851.  
  852. static void
  853. print_frame_arg_vars (frame, stream)
  854.      register FRAME frame;
  855.      register FILE *stream;
  856. {
  857.   struct symbol *func = get_frame_function (frame);
  858.   register struct block *b;
  859.   int nsyms;
  860.   register int i;
  861.   register struct symbol *sym, *sym2;
  862.   register int values_printed = 0;
  863.  
  864.   if (func == 0)
  865.     {
  866.       fprintf_filtered (stream, "No symbol table info available.\n");
  867.       return;
  868.     }
  869.  
  870.   b = SYMBOL_BLOCK_VALUE (func);
  871.   nsyms = BLOCK_NSYMS (b);
  872.  
  873.   for (i = 0; i < nsyms; i++)
  874.     {
  875.       sym = BLOCK_SYM (b, i);
  876.       if (SYMBOL_CLASS (sym) == LOC_ARG
  877.       || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
  878.       || SYMBOL_CLASS (sym) == LOC_REF_ARG
  879.       || SYMBOL_CLASS (sym) == LOC_REGPARM)
  880.     {
  881.       values_printed = 1;
  882.       fprint_symbol (stream, SYMBOL_NAME (sym));
  883.       fputs_filtered (" = ", stream);
  884.       /* We have to look up the symbol because arguments often have
  885.          two entries (one a parameter, one a register) and the one
  886.          we want is the register, which lookup_symbol will find for
  887.          us.  */
  888.       sym2 = lookup_symbol (SYMBOL_NAME (sym),
  889.             b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
  890.       print_variable_value (sym2, frame, stream);
  891.       fprintf_filtered (stream, "\n");
  892.     }
  893.     }
  894.  
  895.   if (!values_printed)
  896.     {
  897.       fprintf_filtered (stream, "No arguments.\n");
  898.     }
  899. }
  900.  
  901. static void
  902. args_info (ignore, from_tty)
  903.      char *ignore;
  904.      int from_tty;
  905. {
  906.   if (!selected_frame)
  907.     error ("No frame selected.");
  908.   print_frame_arg_vars (selected_frame, stdout);
  909. }
  910.  
  911. /* Select frame FRAME, and note that its stack level is LEVEL.
  912.    LEVEL may be -1 if an actual level number is not known.  */
  913.  
  914. void
  915. select_frame (frame, level)
  916.      FRAME frame;
  917.      int level;
  918. {
  919.   register struct symtab *s;
  920.  
  921.   selected_frame = frame;
  922.   selected_frame_level = level;
  923.  
  924.   /* Ensure that symbols for this frame are read in.  Also, determine the
  925.      source language of this frame, and switch to it if desired.  */
  926.   if (frame)
  927.   {
  928.     s = find_pc_symtab (get_frame_info (frame)->pc);
  929.     if (s 
  930.     && s->language != current_language->la_language
  931.     && s->language != language_unknown
  932.     && language_mode == language_mode_auto) {
  933.       set_language(s->language);
  934.     }
  935.   }
  936. }
  937.  
  938. /* Store the selected frame and its level into *FRAMEP and *LEVELP.
  939.    If there is no selected frame, *FRAMEP is set to NULL.  */
  940.  
  941. void
  942. record_selected_frame (frameaddrp, levelp)
  943.      FRAME_ADDR *frameaddrp;
  944.      int *levelp;
  945. {
  946.   *frameaddrp = selected_frame ? FRAME_FP (selected_frame) : 0;
  947.   *levelp = selected_frame_level;
  948. }
  949.  
  950. /* Return the symbol-block in which the selected frame is executing.
  951.    Can return zero under various legitimate circumstances.  */
  952.  
  953. struct block *
  954. get_selected_block ()
  955. {
  956.   if (!target_has_stack)
  957.     return 0;
  958.  
  959.   if (!selected_frame)
  960.     return get_current_block ();
  961.   return get_frame_block (selected_frame);
  962. }
  963.  
  964. /* Find a frame a certain number of levels away from FRAME.
  965.    LEVEL_OFFSET_PTR points to an int containing the number of levels.
  966.    Positive means go to earlier frames (up); negative, the reverse.
  967.    The int that contains the number of levels is counted toward
  968.    zero as the frames for those levels are found.
  969.    If the top or bottom frame is reached, that frame is returned,
  970.    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
  971.    how much farther the original request asked to go.  */
  972.  
  973. FRAME
  974. find_relative_frame (frame, level_offset_ptr)
  975.      register FRAME frame;
  976.      register int* level_offset_ptr;
  977. {
  978.   register FRAME prev;
  979.   register FRAME frame1;
  980.  
  981.   /* Going up is simple: just do get_prev_frame enough times
  982.      or until initial frame is reached.  */
  983.   while (*level_offset_ptr > 0)
  984.     {
  985.       prev = get_prev_frame (frame);
  986.       if (prev == 0)
  987.     break;
  988.       (*level_offset_ptr)--;
  989.       frame = prev;
  990.     }
  991.   /* Going down is just as simple.  */
  992.   if (*level_offset_ptr < 0)
  993.     {
  994.       while (*level_offset_ptr < 0) {
  995.     frame1 = get_next_frame (frame);
  996.     if (!frame1)
  997.       break;
  998.     frame = frame1;
  999.     (*level_offset_ptr)++;
  1000.       }
  1001.     }
  1002.   return frame;
  1003. }
  1004.  
  1005. /* The "select_frame" command.  With no arg, NOP.
  1006.    With arg LEVEL_EXP, select the frame at level LEVEL if it is a
  1007.    valid level.  Otherwise, treat level_exp as an address expression
  1008.    and select it.  See parse_frame_specification for more info on proper
  1009.    frame expressions. */
  1010.  
  1011. /* ARGSUSED */
  1012. static void
  1013. select_frame_command (level_exp, from_tty)
  1014.      char *level_exp;
  1015.      int from_tty;
  1016. {
  1017.   register FRAME frame, frame1;
  1018.   unsigned int level = 0;
  1019.  
  1020.   if (!target_has_stack)
  1021.     error ("No stack.");
  1022.  
  1023.   frame = parse_frame_specification (level_exp);
  1024.  
  1025.   /* Try to figure out what level this frame is.  But if there is
  1026.      no current stack, don't error out -- let the user set one.  */
  1027.   frame1 = 0;
  1028.   if (get_current_frame()) {
  1029.     for (frame1 = get_prev_frame (0);
  1030.      frame1 && frame1 != frame;
  1031.      frame1 = get_prev_frame (frame1))
  1032.       level++;
  1033.   }
  1034.  
  1035.   if (!frame1)
  1036.     level = 0;
  1037.  
  1038.   select_frame (frame, level);
  1039. }
  1040.  
  1041. /* The "frame" command.  With no arg, print selected frame briefly.
  1042.    With arg, behaves like select_frame and then prints the selected
  1043.    frame.  */
  1044.  
  1045. static void
  1046. frame_command (level_exp, from_tty)
  1047.      char *level_exp;
  1048.      int from_tty;
  1049. {
  1050.   select_frame_command (level_exp, from_tty);
  1051.   print_stack_frame (selected_frame, selected_frame_level, 1);
  1052. }
  1053.  
  1054. /* Select the frame up one or COUNT stack levels
  1055.    from the previously selected frame, and print it briefly.  */
  1056.  
  1057. /* ARGSUSED */
  1058. static void
  1059. up_silently_command (count_exp, from_tty)
  1060.      char *count_exp;
  1061.      int from_tty;
  1062. {
  1063.   register FRAME frame;
  1064.   int count = 1, count1;
  1065.   if (count_exp)
  1066.     count = parse_and_eval_address (count_exp);
  1067.   count1 = count;
  1068.   
  1069.   if (!target_has_stack)
  1070.     error ("No stack.");
  1071.  
  1072.   frame = find_relative_frame (selected_frame, &count1);
  1073.   if (count1 != 0 && count_exp == 0)
  1074.     error ("Initial frame selected; you cannot go up.");
  1075.   select_frame (frame, selected_frame_level + count - count1);
  1076. }
  1077.  
  1078. static void
  1079. up_command (count_exp, from_tty)
  1080.      char *count_exp;
  1081.      int from_tty;
  1082. {
  1083.   up_silently_command (count_exp, from_tty);
  1084.   print_stack_frame (selected_frame, selected_frame_level, 1);
  1085. }
  1086.  
  1087. /* Select the frame down one or COUNT stack levels
  1088.    from the previously selected frame, and print it briefly.  */
  1089.  
  1090. /* ARGSUSED */
  1091. static void
  1092. down_silently_command (count_exp, from_tty)
  1093.      char *count_exp;
  1094.      int from_tty;
  1095. {
  1096.   register FRAME frame;
  1097.   int count = -1, count1;
  1098.   if (count_exp)
  1099.     count = - parse_and_eval_address (count_exp);
  1100.   count1 = count;
  1101.   
  1102.   if (!target_has_stack)
  1103.     error ("No stack.");
  1104.  
  1105.   frame = find_relative_frame (selected_frame, &count1);
  1106.   if (count1 != 0 && count_exp == 0)
  1107.     error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
  1108.   select_frame (frame, selected_frame_level + count - count1);
  1109. }
  1110.  
  1111.  
  1112. static void
  1113. down_command (count_exp, from_tty)
  1114.      char *count_exp;
  1115.      int from_tty;
  1116. {
  1117.   down_silently_command (count_exp, from_tty);
  1118.   print_stack_frame (selected_frame, selected_frame_level, 1);
  1119. }
  1120.  
  1121. static void
  1122. return_command (retval_exp, from_tty)
  1123.      char *retval_exp;
  1124.      int from_tty;
  1125. {
  1126.   struct symbol *thisfun;
  1127.   FRAME_ADDR selected_frame_addr;
  1128.   CORE_ADDR selected_frame_pc;
  1129.   FRAME frame;
  1130.  
  1131.   if (selected_frame == NULL)
  1132.     error ("No selected frame.");
  1133.   thisfun = get_frame_function (selected_frame);
  1134.   selected_frame_addr = FRAME_FP (selected_frame);
  1135.   selected_frame_pc = (get_frame_info (selected_frame))->pc;
  1136.  
  1137.   /* If interactive, require confirmation.  */
  1138.  
  1139.   if (from_tty)
  1140.     {
  1141.       if (thisfun != 0)
  1142.     {
  1143.       if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun)))
  1144.         error ("Not confirmed.");
  1145.     }
  1146.       else
  1147.     if (!query ("Make selected stack frame return now? "))
  1148.       error ("Not confirmed.");
  1149.     }
  1150.  
  1151.   /* Do the real work.  Pop until the specified frame is current.  We
  1152.      use this method because the selected_frame is not valid after
  1153.      a POP_FRAME.  The pc comparison makes this work even if the
  1154.      selected frame shares its fp with another frame.  */
  1155.  
  1156.   while ( selected_frame_addr != FRAME_FP (frame = get_current_frame())
  1157.        || selected_frame_pc   != (get_frame_info (frame))->pc  )
  1158.     POP_FRAME;
  1159.  
  1160.   /* Then pop that frame.  */
  1161.  
  1162.   POP_FRAME;
  1163.  
  1164.   /* Compute the return value (if any) and store in the place
  1165.      for return values.  */
  1166.  
  1167.   if (retval_exp)
  1168.     set_return_value (parse_and_eval (retval_exp));
  1169.  
  1170.   /* If interactive, print the frame that is now current.  */
  1171.  
  1172.   if (from_tty)
  1173.     frame_command ("0", 1);
  1174. }
  1175.  
  1176. /* Gets the language of the current frame. */
  1177. enum language
  1178. get_frame_language()
  1179. {
  1180.    register struct symtab *s;
  1181.    FRAME fr;
  1182.    enum language flang;        /* The language of the current frame */
  1183.    
  1184.    fr = get_frame_info(selected_frame);
  1185.    if(fr)
  1186.    {
  1187.       s = find_pc_symtab(fr->pc);
  1188.       if(s)
  1189.      flang = s->language;
  1190.       else
  1191.      flang = language_unknown;
  1192.    }
  1193.    else
  1194.       flang = language_unknown;
  1195.  
  1196.    return flang;
  1197. }
  1198.  
  1199. void
  1200. _initialize_stack ()
  1201. {
  1202. #if 0  
  1203.   backtrace_limit = 30;
  1204. #endif
  1205.  
  1206.   add_com ("return", class_stack, return_command,
  1207.        "Make selected stack frame return to its caller.\n\
  1208. Control remains in the debugger, but when you continue\n\
  1209. execution will resume in the frame above the one now selected.\n\
  1210. If an argument is given, it is an expression for the value to return.");
  1211.  
  1212.   add_com ("up", class_stack, up_command,
  1213.        "Select and print stack frame that called this one.\n\
  1214. An argument says how many frames up to go.");
  1215.   add_com ("up-silently", class_support, up_silently_command,
  1216.        "Same as the `up' command, but does not print anything.\n\
  1217. This is useful in command scripts.");
  1218.  
  1219.   add_com ("down", class_stack, down_command,
  1220.        "Select and print stack frame called by this one.\n\
  1221. An argument says how many frames down to go.");
  1222.   add_com_alias ("do", "down", class_stack, 1);
  1223.   add_com ("down-silently", class_support, down_silently_command,
  1224.        "Same as the `down' command, but does not print anything.\n\
  1225. This is useful in command scripts.");
  1226.  
  1227.   add_com ("frame", class_stack, frame_command,
  1228.        "Select and print a stack frame.\n\
  1229. With no argument, print the selected stack frame.  (See also \"info frame\").\n\
  1230. An argument specifies the frame to select.\n\
  1231. It can be a stack frame number or the address of the frame.\n\
  1232. With argument, nothing is printed if input is coming from\n\
  1233. a command file or a user-defined command.");
  1234.  
  1235.   add_com_alias ("f", "frame", class_stack, 1);
  1236.  
  1237.   add_com ("select-frame", class_stack, select_frame_command,
  1238.        "Select a stack frame without printing anything.\n\
  1239. An argument specifies the frame to select.\n\
  1240. It can be a stack frame number or the address of the frame.\n");
  1241.  
  1242.   add_com ("backtrace", class_stack, backtrace_command,
  1243.        "Print backtrace of all stack frames, or innermost COUNT frames.\n\
  1244. With a negative argument, print outermost -COUNT frames.");
  1245.   add_com_alias ("bt", "backtrace", class_stack, 0);
  1246.   add_com_alias ("where", "backtrace", class_alias, 0);
  1247.   add_info ("stack", backtrace_command,
  1248.         "Backtrace of the stack, or innermost COUNT frames.");
  1249.   add_info_alias ("s", "stack", 1);
  1250.   add_info ("frame", frame_info,
  1251.         "All about selected stack frame, or frame at ADDR.");
  1252.   add_info_alias ("f", "frame", 1);
  1253.   add_info ("locals", locals_info,
  1254.         "Local variables of current stack frame.");
  1255.   add_info ("args", args_info,
  1256.         "Argument variables of current stack frame.");
  1257.   add_info ("catch", catch_info,
  1258.         "Exceptions that can be caught in the current stack frame.");
  1259.  
  1260. #if 0
  1261.   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, 
  1262.        "Specify maximum number of frames for \"backtrace\" to print by default.",
  1263.        &setlist);
  1264.   add_info ("backtrace-limit", backtrace_limit_info,
  1265.         "The maximum number of frames for \"backtrace\" to print by default.");
  1266. #endif
  1267. }
  1268.